package com.jielihaofeng;

import com.jielihaofeng.chat.config.EnableLocalCache;
import com.jielihaofeng.chat.config.NettyConfig;
import com.jielihaofeng.chat.server.WebSocketNettyServer;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
@EnableLocalCache
public class ServerApplication extends SpringBootServletInitializer implements CommandLineRunner {

	private final WebSocketNettyServer webSocketNettyServer;

	private final NettyConfig nettyConfig;

	public ServerApplication(WebSocketNettyServer webSocketNettyServer, NettyConfig nettyConfig) {
		this.webSocketNettyServer = webSocketNettyServer;
		this.nettyConfig = nettyConfig;
	}

	public static void main(String[] args) {
		SpringApplication.run(ServerApplication.class, args);
	}

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		return application.sources(ServerApplication.class);
	}

	@Override
	public void run(String... args) throws Exception {
		new Thread(() -> webSocketNettyServer.start(nettyConfig.getPort())).start();
	}
}

